<template>
  <ContentWrap>
    <el-form :model="queryParams" ref="queryFormRef" label-width="auto" :inline="true">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item label="入住编号" prop="checkInNo">
        <el-input
          v-model="queryParams.checkInNo"
          placeholder="请输入入住编号"
          :clearable="true"
          @keyup.enter="getPage"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['elder:checkIn:query']" @click="handleClickSearch">
          <Icon icon="ep:search" />
          搜索
        </el-button>
        <el-button @click="handleClickReset">
          <Icon icon="ep:refresh" />
          重置
        </el-button>
        <el-button v-hasPermi="['elder:checkIn:create']" @click="handleClickCreate">
          <Icon icon="ep:plus" />
          新增
        </el-button>
      </el-form-item>
    </el-form>
    <Table
      :columns="columns"
      :page-param="queryParams"
      :adaptive="true"
      :page-data="queryData"
      save-key="ElderCheckIn"
      height-per="90"
      @page-change="getPage"
      @row-dblclick="handleTableDblclick"
    >
      <template #menu="{ row }">
        <context-menu-item
          label="修改"
          @click="handleMenuUpdate(row)"
          v-hasPermi="['elder:checkIn:update']"
        />
        <context-menu-item
          label="确认入住"
          @click="handleMenuConfirm(row)"
          v-hasPermi="['elder:checkIn:confirm']"
        />

        <context-menu-item
          label="取消入住"
          @click="handleMenuCancel(row)"
          v-hasPermi="['elder:checkIn:cancel']"
        />
      </template>
    </Table>
  </ContentWrap>
  <ElderCheckInEdit ref="editFormRef" @success="getPage" />
</template>
<script setup name="ElderCheckIn" lang="ts">
import { formatDate } from '@/utils/formatTime'
import * as ElderCheckInApi from '@/api/elder/checkIn'
import ElderCheckInEdit from './ElderCheckInEdit.vue'
import { ElderCheckInStatusEnum } from '@/utils/constants'
import { DICT_TYPE, getDictObj } from '@/utils/dict'
const message = useMessage()
const editFormRef = ref()
const queryFormRef = ref()
const queryParams = ref({
  name: undefined,
  checkInNo: undefined
})
const queryData = ref()
const columns = [
  {
    label: '入住编号',
    prop: 'checkInNo'
  },
  {
    label: '姓名',
    prop: 'elder.name'
  },
  {
    label: '身份证号',
    prop: 'elder.identityCard'
  },
  {
    label: '状态',
    prop: 'status',
    formatter: ({ status }) => getDictObj(DICT_TYPE.ELDER_CHECK_IN_STATUS, status).label
  },
  {
    label: '入住开始时间',
    prop: 'beginTime',
    formatter: ({ beginTime }) => formatDate(beginTime, 'YYYY-MM-DD')
  },
  {
    label: '入住预计结束时间',
    prop: 'endTime',
    formatter: ({ endTime }) => formatDate(endTime, 'YYYY-MM-DD')
  },
  {
    label: '床位名称',
    prop: 'bedInfo.bedName'
  },
  {
    label: '护理计划名称',
    prop: 'nursePlan.planName'
  },
  {
    label: '餐饮套餐名称',
    prop: 'restaurantCombo.comboName'
  },
  {
    label: '入住实交费用',
    prop: 'checkInFee'
  },
  {
    label: '减免费用',
    prop: 'reductionFee'
  },
  {
    label: '创建时间',
    prop: 'createTime'
  }
]
/** 初始化 */
onMounted(() => {
  getPage()
})

/** 搜索按钮操作 */
const handleClickSearch = () => {
  getPage()
}

/** 重置按钮操作 */
const handleClickReset = () => {
  queryFormRef.value?.resetFields()
  handleClickSearch()
}

/** 新增按钮操作 */
const handleClickCreate = () => {
  editFormRef.value.open('create', 0)
}

/** 处理响应事件  */
const getPage = async () => {
  queryData.value = await ElderCheckInApi.query(queryParams.value)
}

/** 处理右击事件 修改 */
const handleMenuUpdate = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理tables双击修改事件 */
const handleTableDblclick = (row) => {
  editFormRef.value.open('update', row.id)
}

/** 处理右击事件 确认入住 */
const handleMenuConfirm = async (row) => {
  await message.confirm('确定要入住吗？')
  await ElderCheckInApi.updateStatus({ id: row.id, status: ElderCheckInStatusEnum.CHECKED_IN })
  getPage()
  message.success('确认入住成功')
}

/** 处理右击事件 确认取消 */
const handleMenuCancel = async (row) => {
  await message.confirm('确定要取消入住吗？')
  await ElderCheckInApi.updateStatus({ id: row.id, status: ElderCheckInStatusEnum.CANCEL })
  getPage()
  message.success('取消入住成功')
}
</script>
